
BlInking and
Repeating on the
Model I
	by Craig A Lindley	
      
	System Requirements:
	Model I
	16K RAM
	One disk drive
	TRSDOS-compatible DOS


The Model I, unlike newer and more expensive computers, lacks a
blinking cursor and repeating keys. This Utility adds these capabilities
to your Model 1. Like most other special keyboard drive routines, 
NEWDVR patches itself into the keyboard device control block (DCB)
so that it, not the normal keyboard driver routine, is executed when the
keyboard is polled. The computer is continually polling the keyboard in
order to respond to input immediately.
Essentially, NEWDVR directs the computer to:
Blink cursor on then off until a key is pressed.
Transmit the valid key code immediately to the operating system. 
If the key is held down, delay before repeating the key.
After delay is up, repeat key until key is released.
I have successfully tested this routine in BASIC and machine-language programs.

How It Works
The first portion of code, labeled PATCH, puts the address of the 
NEWDVR routine into the keyboard DCB (see Program Listing). The 
PATCH routine also places the address of the normal keyboard scan 
routine, taken from the DCB, into the NEWDVR code at two places, so you
can call the keyboard scan routine to get keyboard characters from the
operator. PATCH also protects NEWDVR from being overwritten, by
storing its address, minus one, in the DOS top-of-memory pointer at
4049H.
The first part of the NEWDVR code checks to see if the routine calling
the keyboard scan is the wait-for-key routine at 49H in ROM. If so, the
blinking cursor routine is executed. If not, the normal key scan routine is
initiated. This function is checked by looking back on the stack to see
what the return address of the calling routine is. An address of 4CH, ten
bytes back on the stack, indicates the routine at 49H is calling. Any other
address indicates the calling program is doing a keyboard scan, probably
just to check whether the BREAK key is active, not waiting for input. 
This feature was added because quite a few TRS-80 programs scan the
keyboard constantly for a BREAK command. This is normally invisible,
but becomes painfully obvious if the cursor has to blink before the calling
program can continue execution.
If the NEWDVR routine decides that the blinking cursor routine 
should be executed, the first thing it does is turn off the normal cursor
character, by outputting the cursor-off character code of 15 decimal to
the ROM display routine at 33H. Then the address of the cursor is loaded
into HL and the character at the current address of the cl>sor is loaded
into register C for safekeeping. The new block cursor character (code
143 decimal) is stored over the original character at the current address
of the cursor. The RDKEYS routine, described below, is called to read
the keyboard repeatedly until either a key press is detected or a 
predetermined time period elapses. This time period is cQntrolled by the 
BLINK equate in the listing.
The RDKEYS routine returns with the Z flag reset, if a key was detected;
or set, if not. The original character at the cursor position is replaced.
If the RDKEYS routine detected a key, control returns to the operating
system with that character in the A register. If not, the RDKEYS routine
is again called to see if a key has now been pressed. Control returns 
unconditionally to the operating system after this call, with the Z flag set 
accordingly. Calling the RDKEYS subroutine twice, first with the,block
cursor character at the cursor address, and then with the original character
at the cursor address, causes the cursor to blink at a 50/50 duty cycle
(half on, half off).
The RDKEYS routine performs the actual polling of the keyboard. The
number of times it polls before returning to the NEWDVR routine is 
controlled by the BLINK time constant, which is contained in DC. This
count decrements one each time the keyboard is polled and no key has been
pressed. The delay count (IX) and the first-time flag (IX 1) are reset to
zero to indicate that a key was not found. The delay count determines how
long after the key is held down the repeat function begins. The first-time
flag is zero before a new key is pressed, and one afterwards.
If the BLINK count reaches OFFH, or 255, before a key is detected, the 
RDKEYS routine returns to the NEWDVR routine with Z =1 indicating 
this. Clearing the 7-byte keyboard work area at 4036H to zero before
polling the keyboard causes the normal keyboard driver routine to return
a key code, even if the key has not been released. Normally, the 
keyboard driver routine does not return a key code until another key is
pressed, or the original key was released and pressed again.
If the RDKEYS routine determines that a key has been pressed before
the BLINK count is exhausted, control transfers to the FOUND routine
for additional processing. There, the key character code is placed in the
B register for temporary storage. The delay count is loaded into the 
accumulator and a comparison is done to see if the count equals zero. The
count equals zero when a key is first pressed, or after the delay time
counter has overflowed. If a key was just pressed, control transfers to F1,
and the keyboard character stored in B is returned to the A register.
A test is performed to see whether this is a new keypress, signified by
the first-time flag equaling zero. If so, the delay count is incremented to
one and the first time flag is set to one, so that these instructions are
skipped the next time the Fl routine is entered. The F2 routine is executed
whether or not this was the first time through Fl. F2 ~resets the Z flag to
indicate that a key has been pressed, before returning the key character in
the A register to the NEWDVR routine and eventually to the operating system.
If the key remains pressed the next time the keyboard is polled, control does
not pass to the Fl routine because the delay count is not zero. This causes
the delay count to shift, the accumulator to clear, the Z flag to be set, 
and control to return to the NEWDVR routine. This means a delay before the
keys start to repeat, because when the delay count doesnt equal zero, 
this routine passes the keyboard null code of 00 back to the operating 
system, just as if no keys were being pressed. When shifting the count 
causes the count to again equal zero, the Fl routine is executed again. 
This time, however, the Fl routine doesnt increment the delay count, so the
keys repeat at full speed.

Using this Program
Place this program in memory one byte at a time, by using a monitor 
program such as DEBUG and writing it to tape or disk. Or, assemble it as
a DOS/CMD file, using an editor/assembler. Change the program ORG,
as shown on the listing, to reflect your computers memory size. If your
system is non-disk, change the jump to the operating system in line 250
of the listing to a return to BASIC (1A19H). Make sure the memory location
of this program doesnt conflict with any other high memory programs.


